/*
 * @Descripttion: 学生模块控制器
 * @version: 
 * @Author: hzx
 * @Date: 2021-02-26 16:12:39
 * @LastEditors: hzx
 * @LastEditTime: 2021-05-14 16:05:26
 */
'use strict';

const Controller = require('egg').Controller;

class StudentController extends Controller {
    async index() {
        this.ctx.body = "api hi"
    }

    /**
     * @name: 获取所有学生信息
     * @msg: '学生信息查询成功'
     * @param { object } stu_id非必传
     * @return {*}
     */
    async getStudents() {
        let { stu_id } = this.ctx.query
        var sql = "select *,FROM_UNIXTIME(rx_time,'%Y-%m-%d') as rx_time from student where 1=1 ";
        if (stu_id) {
            var sql = sql + ` AND stu_id = ${stu_id}`
        }
        const results = await this.app.mysql.query(sql);
        //console.log(sql);
        this.ctx.body = {
            code: '200',
            msg: '学生信息查询成功',
            data: results,
            result: true
        }
    }

    /**
     * @name: 根据学生id查询学生信息
     * @msg: '学生信息查询成功'
     * @param { stu_id }
     * @return {*}
     */
    async selectStudentId() {
        let { stu_id } = this.ctx.params;
        const sql = `select *,FROM_UNIXTIME(rx_time,'%Y-%m-%d') as rx_time from student left join class on student.class_id = class.class_id where stu_id=${stu_id}`;
        //console.log(sql);
        let results = await this.app.mysql.query(sql);
        this.ctx.body = {
            code: '200',
            msg: '学生信息查询成功',
            data: results,
            result: true
        }
    }

    /**
     * @name: 查询班级id查询班级所有学生
     * @msg: '查询成功'
     * @param { class_id }
     * @return {*}
     */

    async selectClassId() {
        let { class_id } = this.ctx.params;
        const sql = `select *,FROM_UNIXTIME(rx_time,'%Y-%m-%d') as rx_time from student where class_id=${class_id}`;
        //console.log(sql);
        let results = await this.app.mysql.query(sql);
        this.ctx.body = {
            code: '200',
            msg: '查询成功',
            data: results,
            result: true
        }
    }

    /**
     * @name: 增加学生信息
     * @msg:  '插入成功'
     * @param {*} stu_id, class_id, stu_sex, rx_time, stu_name 必填
     * @return {*}
     */
    async insterStudent() {
        let { stu_id, class_id, stu_sex, rx_time, stu_name } = this.ctx.request.body;
        //console.log(this.ctx.request.body)
        let sql = `insert into student(stu_id, class_id, stu_sex, rx_time, stu_name) values('${stu_id}', '${class_id}', '${stu_sex}', ${rx_time}, '${stu_name}')`
        const result = await this.app.mysql.query(sql);
        const insertSuccess = result.affectedRows === 1;
        const insertId = stu_id
        //console.log(insertSuccess)
        this.ctx.body = {
            isSuccess: insertSuccess,
            insertId: insertId,
            code: '200',
            msg: '插入成功',
            result: true
        }
    }

    /**
     * @name: 修改学生信息
     * @msg:  '修改成功'
     * @param {*} stu_id, class_id, stu_sex, rx_time, stu_name 选填
     * @return {*}
     */
    async updateStudent() {
        let { t_id, stu_id, class_id, stu_sex, rx_time, stu_name, position, association, dormitory, info, member } = this.ctx.request.body;
        // 这里通过t_id判断是老师修改还是学生修改，老师不可改学生部分信息
        if (t_id) {
            var sql = `update student set class_id = '${class_id}', stu_sex = '${stu_sex}', rx_time = '${rx_time}', stu_name = '${stu_name}' where stu_id = '${stu_id}'`
        } else {
            var sql = `update student set class_id = '${class_id}', position='${position}', member='${member}', association='${association}', dormitory='${dormitory}', info='${info}', stu_sex = '${stu_sex}', rx_time = '${rx_time}', stu_name = '${stu_name}' where stu_id = '${stu_id}'`
        }
        const result = await this.app.mysql.query(sql);
        const updateSuccess = result.affectedRows === 1;
        //console.log(updateSuccess)
        this.ctx.body = {
            isSuccess: updateSuccess,
            code: '200',
            msg: '修改成功',
            result: true
        }
    }


    /**
     * @name: 根据id删除信息
     * @msg: 
     * @param {*} stu_id
     * @return {*}
     */
    async delStudent() {
        let stu_id = this.ctx.params.stu_id;
        //console.log(stu_id)
        const res = await this.app.mysql.delete('student', { 'stu_id': stu_id });
        //console.log(res)
        this.ctx.body = {
            code: '200',
            data: res,
            msg: '删除成功',
            result: true
        }
    }

}

module.exports = StudentController
